//
//  AOUserAccountViewModel.swift
//  weibo_zha
//
//  Created by _zha on 16/10/25.
//  Copyright © 2016年 渣. All rights reserved.
//

import UIKit

class AOUserAccountViewModel {
    //全局访问点
    static let sharedTools: AOUserAccountViewModel = AOUserAccountViewModel()
    //用户信息对象
    var userAccountModel: AOUserAccountModel?
    //路径
    let file = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("userAccount.archiver")
    
    var isLogin:Bool{
        return accessToken != nil
    }
    
    /// 访问令牌
    var accessToken:String?{
        //
        if userAccountModel?.expires_Date?.compare(Date()) == ComparisonResult.orderedDescending {
            return userAccountModel?.access_token
        }else {
            return nil
        }
    }
    
    init() {
        //用户模型赋值
        userAccountModel = getUserAccountModel()
    }
    
}

///
extension AOUserAccountViewModel {
    // 获取 token
    func getUserAccount(code: String, finish:@escaping (Bool)->()){
        // 发送请求
        AONetworkTools.sharedTools.oauthLoadUserAccount(code: code, success: { (response) in
            //
            guard let res = response as?[String: Any] else{
                finish(false)
                return
            }
            // 字典转模型
            let userAccountModel = AOUserAccountModel.yy_model(withJSON: res)
            
            // 判断userAccountModel 不为 nil
            guard let model = userAccountModel else {
                finish(false)
                return
            }
            // 请求用户信息
            self.getUserInfo(model: model, finish: finish)
        }) { (error) in
            finish(false)
            print("请求失败",error)
        }
    }
    
    // 请求用户信息
    func getUserInfo(model: AOUserAccountModel, finish:@escaping (Bool)->()){
        // 发送请求
        AONetworkTools.sharedTools.oauthLoadUserInfo(model: model, success: { (response) in
            // 判断response 是否为 nil 而且是否可以转成一个字典
            guard let res = response as?[String: Any] else{
                finish(false)
                return
            }
            //
            model.screen_name = res["screen_name"] as? String
            model.avatar_large = res["avatar_large"] as? String
            // 保存模型
            self.saveUserAccountModel(model: model)
            // 最终的成功的执行闭包
            finish(true)
        }) { (error) in
            finish(false)
            print("请求失败",error)
        }
    }
}

//归档解档
extension AOUserAccountViewModel {
   
    // 保存用户信息对象
    func saveUserAccountModel(model: AOUserAccountModel){
        //
        userAccountModel = model
        NSKeyedArchiver.archiveRootObject(model, toFile: file)
    }
    // 获取用户信息对象
    fileprivate func getUserAccountModel() ->AOUserAccountModel?{
        let result = NSKeyedUnarchiver.unarchiveObject(withFile: file) as? AOUserAccountModel
        return result
    }
    
}




